define(['app','pagination','datetime','editor'], function( app ) {
'use strict';
//报价管理
app.controller('ProductPrice', function($scope, dataFactoryCRM, $q) {
	$scope.num = "";
	$scope.row = "";
	$scope.sub_company="";
	$scope.sub_product = "";
	$scope.search_service_type ="";
	$scope.search_sub_product ="";
	$scope.search_sub_company= "";
	$scope.add_product_name = "";
	$scope.add_service_time = "";
	$scope.add_discount_price="0";
	$scope.add_discount="0";
	$scope.add_free_price="0";
	$scope.save_check = "0";
	$scope.vvvv ="0";
	$scope.yy ="0";
	$scope.kkv = 0;
	$scope.ProductPricePagess = {pageAll: 1,pageNow: 1,pageSize: 10,k: 3};
	$('.ui.selection.dropdown').dropdown();
	$scope.busy = dataFactoryCRM.getDataByAPI('ProductPrice/echk').then(
	  	function( data ){
	  		$scope.busy = false;
			$scope.search_product = data.product;
			$scope.search_company = data.company;
			$scope.user = data.user;
			if($scope.user.user_id == 1){
				$("#sub_company").show();
			}else{
				$("#sub_company").hide();
			}

		}
  	);

	$scope.ProductPriceList = function() {
		$scope.$emit("BUSY");
		if($("#search_sub_product").val() == "null"){$scope.search_sub_product = "";}else{$scope.search_sub_product = $("#search_sub_product").val();}
		if($("#search_sub_company").val() == "null"){$scope.search_sub_company = "";}else{$scope.search_sub_company = $("#search_sub_company").val();}
		if($("#search_service_type").val() == undefined ){
			$scope.search_service_type = "";
		}else{
			$scope.search_service_type = $("#search_service_type").val();
		}
		var _url ='ProductPrice/product_price?company='+$scope.search_sub_company+'&product='+$scope.search_sub_product+'&type='+$scope.search_service_type+'&p=' + $scope.ProductPricePagess.pageNow + '&psize=' + $scope.ProductPricePagess.pageSize;
		 dataFactoryCRM.getDataByAPI(_url).then(
	        function(data){
	        	if(data.rcode=="2"){
	        		$("#y_product_list").hide();
	        		$("#w_product_list").show();
	        		$("#w_product_lister").hide();
	        		$scope.ProductPricePagess.pageAll = 1;
	        	}else{
	        		$("#y_product_list").show();$("#w_product_list").hide();
	        		$("#w_product_lister").show();
	        		$scope.product_price = data.data;
		          	$scope.ProductPricePagess.pageSize = data.psize;
					$scope.ProductPricePagess.pageAll = data.page;
					$scope.ProductPricePagess.pageNow = data.p;
	        	}
	        	$scope.$emit("NOTBUSY");
	        }
	    )
    }
	$scope.ProductPriceList();

	$scope.ProductPrice = function(){
		$scope.ProductPriceList($scope.sub_company,$scope.sub_product,$scope.search_service_type);
	}

	$scope.showAdd = function() {
		//加载添加模板
	    $('.ui.product_price_add').modal({
	      onApprove: function() {
	 
	        if( $('#frm-add').form('is valid') && !isNaN($scope.add_free_time) && !isNaN($scope.add_price) && !isNaN($scope.add_service_price) && $scope.add_service_time >= $scope.add_free_time && $scope.add_price >= $scope.add_service_price){
	        	$scope.product_price_add( $('#frm-add').serialize() );	
	        }else{
	             return false;
	         }
	      }
	    }).modal('show');
	    dataFactoryCRM.getDataByAPI('ProductPrice/notStandarder').then(
			function( data ){
				$("#adefault_product").text("所属产品");
				$("#add_product_name").val("");
				$("#default_service_time").text("服务期类型");
				$("#add_service_time").val("");
				$("#frm-add input").val("");
				$("#frm-add textarea").val("");

				$scope.product_name = data.product;
				$scope.service_time = data.service_time;
				$scope.free_time = data.free_time;
			});
	  }

	$scope.product_price_add = function(_d){
		//产品报价添加
		$scope.$emit("BUSY");
		$scope.add_product_id = $("#add_product_name").val();
		var _var = _d + '&product_id='+ $scope.add_product_id;
		dataFactoryCRM.postDataByAPI('ProductPrice/product_price_add',_var)                                        
	      	.then( function( data ) {
	          if(data.rcode == "1"){
					// $scope.productMsg = "添加成功!!!";
					// $scope.productRcode = 1;
		    		$scope.ProductPriceList();
		    		return true;
	           }else{
	           		$scope.productMsg = data.rcode;
		    		$scope.productRcode = 1;
	    		    $scope.msgAttention(); 
	           }
		   		$scope.$emit("NOTBUSY");
       		}
        )
	}
	
	$scope.save_product_price = function(key){
		//修改报价加载
		$scope.key = key;
		var _vproduct = this.list;
        $('#frm-save input[name="save_name"]').val(_vproduct.name);
        $('#frm-save input[name="save_price"]').val(parseInt(_vproduct.free_price) + parseInt(_vproduct.service_price));
        $('#frm-save input[name="save_service_price"]').val(_vproduct.service_price);
        $('#frm-save input[name="save_free_time"]').val(_vproduct.free_time);
        $('#frm-save input[name="save_note"]').val(_vproduct.note);
        $('#frm-save input[name="save_product_name"]').val(_vproduct.product_id);
		dataFactoryCRM.getDataByAPI('ProductPrice/notStandarder?price_id='+$scope.key).then(
			function( data ){
				$("#save_price").text("");
				 if(data.save_price.service_price=="" || data.save_price.service_price =="0"){data.save_price.service_price="0";}
				 if(data.save_price.free_time=="" || data.save_price.free_time=="0"){data.save_price.free_time='0'}
				 $scope.discount = (parseInt(data.save_price.service_price) / (parseInt(data.save_price.service_time) + parseInt(data.save_price.free_time))) / ((parseInt(data.save_price.free_price) / parseInt(data.save_price.service_time))) * 10;
				 $scope.eidt_product = data.product;
				 $scope.eidt_service_time = data.service_time;
				 $scope.arr_price = data.save_price;
				 $('#save_name').val(data.save_price.name);
				for(var i=0;i<$scope.eidt_service_time.length;i++){
					if($scope.eidt_service_time[i].id == _vproduct.service_time){
						$("#save_service_time").val(_vproduct.service_time);
					}
				}
			}
		);

		$('.ui.product_price_save').modal({
	      onApprove: function() {
			if($('#frm-save').form('is valid')){
				$scope.check_product_price($('#frm-save').serialize());
			}else{
				return false;
			}	      
	      }
	      
	    }).modal('show');
	}

	$scope.check_product_price = function(_d){
		//报价修改
		 $scope.$emit("BUSY");
		var _var = _d+"&price_id=" +$scope.arr_price.price_id;	
		dataFactoryCRM.postDataByAPI('ProductPrice/save_product_price',_var)                                        
		    .then( function( data ) {
		    	console.log(data);
		    	if(data.rcode == "1"){
		    		$scope.ProductPriceList();
		    		return true;
	           	}else {
	           		$scope.productMsg = data.msg;
		    		$scope.productRcode = 0;
		    		$scope.msgAttention(); 
	           	}
	           	$scope.$emit("NOTBUSY");
	       }); 
	}

	$scope.delete = function(key,name,product){
		//删除提醒
		$('#delete .content p').html("确认删除【"+name+" "+product+"】报价信息吗？" );
		$('.ui.small.del').modal('show');
		$scope.delete_id = key;
	}

	$scope.del_product_price = function(id){
		 $scope.$emit("BUSY");
		dataFactoryCRM.getDataByAPI('Product_price/Product_price_delete?price_id='+id).then(
			function( data ){
				if(data.data == '0'){
					$scope.ProductPriceList();
		    		return true;
				}else{
					$scope.productMsg = "此报价可能含有合同，不能删除!";
		    		$scope.productRcode = 0;
		    		$scope.msgAttention();
				}
				$scope.$emit("NOTBUSY");
			}
		);
	}
	/*系统调用*/
	$scope.msgAttention = function() {
		$('.ui.systemerror').modal({
			closable: false,
			onDeny: function() {
				window.alert('Wait not yet!');
				return false;
			},
			onApprove: function() {
				$('.ui.dimmer.modals.page.transition.visible.active.dimmers').removeClass('dimmers');
			}
		}).modal('show');

		setTimeout(function() {
			var system = $('.ui.dimmer.modals.page.transition.visible.active');
			if (system) {
				$('.ui.dimmer.modals.page.transition.visible.active').addClass('dimmers');
			}
		}, 1000);
	};
	$scope.check_price = function(){
		//添加计算折扣
		if( $("#add_service_time").val() != "" ){
			$scope.kkv = $("#add_service_time").val() ;
		}
			
		if($scope.kkv == "12m"){
			$scope.add_service_time = 12;
		}else if($scope.kkv == "6m"){
			$scope.add_service_time = 6;
		}else if($scope.kkv == "3m"){
			$scope.add_service_time = 3;
		}else if($scope.kkv == "1m"){
			$scope.add_service_time = 1;
		}else{
			$scope.add_service_time = 0;
		}
		if($("#add_price").val() !=""){
			if(!isNaN($("#add_price").val())){
				$scope.add_price = $("#add_price").val();
			}else{
				$scope.add_price = 0;
			}
		}else{
			$scope.add_price = 0;
		}

		if( $("#add_service_price").val() != "" && $("#add_service_price").val() != undefined ){
			if(!isNaN($("#add_service_price").val())){
				$scope.add_service_price = parseInt($("#add_service_price").val());
			}else{
				$scope.add_service_price = 0;
			}
		}else{
			if($("#add_price").val()!="" && $("#add_price").val() != undefined && !isNaN($("#add_price").val())){
				$scope.add_service_price = $scope.add_price;
			}else{
				$scope.add_service_price = 0;
			}
		}
		
		if($("#add_free_time").val() !="" && $("#add_free_time").val() != undefined && !isNaN($("#add_free_time").val())){
			$scope.add_free_time = parseInt($("#add_free_time").val());
		}else{
			$scope.add_free_time = 0;
		}
		if($scope.add_free_time == "0" || $scope.add_service_price =="0"){
			$scope.add_discount = 0;
		}

		if($scope.add_price != "" && $scope.add_service_price != "" && $scope.add_price >= $scope.add_service_price){

			$("#add_free_price").removeClass("error");
			if($scope.add_service_time == 0  || $scope.add_price == 0){
				$scope.add_discount = 0;
			}else{
				$scope.add_discount = ($scope.add_service_price/($scope.add_service_time + $scope.add_free_time))/($scope.add_price/$scope.add_service_time) * 10;
			}
		}else{
			$scope.add_discount = 0;
			$("#add_free_price").addClass("error");
		}		
	}

	$scope.save_check_price = function(){
		//修改计算折扣
		if( $("#save_service_time").val() == "" || $("#save_service_time").val() == undefined ){
			$scope.skkv = $scope.arr_price.service_time;
		}else{
			$scope.skkv = $("#save_service_time").val() ;
		}

		if($scope.skkv == "12m"){
			$scope.save_service_time = 12;
		}else if($scope.skkv == "6m"){
			$scope.save_service_time = 6;
		}else if($scope.skkv == "3m"){
			$scope.save_service_time = 3;
		}else if($scope.skkv == "1m"){
			$scope.save_service_time = 1;
		}else{
			$scope.save_service_time = 0;
		}
		
		if($("#save_price").val() !="" && $("#save_price").val() != undefined){
			$scope.save_price = parseInt($("#save_price").val());
		}else{
			$scope.save_price = $scope.arr_price.free_price;
		}
		if( $("#save_service_price").val() != "" && $("#save_service_price").val() != undefined && $("#save_service_price").val() <= $scope.save_price){
			$("#save_service_price2").removeClass("error");
			$scope.save_service_price = parseInt($("#save_service_price").val());
		}else{
			$("#save_service_price2").addClass("error");
			$scope.save_service_price = 0;
		}
		if($("#save_free_time").val() == ""){
			$("#save_free_time").val('0');
		}

		if($("#save_free_time").val() !="" && $("#save_free_time").val() != undefined && parseInt($("#save_free_time").val()) <= parseInt($scope.save_service_time)){
			$("#check_month").removeClass("error");
			$scope.save_free_time = parseInt($("#save_free_time").val());
		}else{
			alert("优惠后延长服务月数不能大于服务类型!");
			$("#check_month").addClass("error");
			
		}

		if($scope.save_price != "" && $scope.save_service_price != ""){
			$scope.discount = ($scope.add_service_price/($scope.save_service_time + $scope.save_free_time))/($scope.save_price/$scope.save_service_time) * 10;
		}

		if($scope.save_service_time != "" && $scope.add_free_time != ""){
			$scope.discount = ($scope.save_service_price/($scope.save_service_time + $scope.save_free_time))/($scope.save_price/$scope.save_service_time) * 10;
		}
		if($scope.save_price != "" && $scope.add_service_price != "" && $scope.save_service_time != ""){
			$scope.discount = ($scope.save_service_price/($scope.save_service_time + $scope.save_free_time))/($scope.save_price/$scope.save_service_time) * 10;
		}

		if($scope.save_price != "" && $scope.add_service_price != "" && $scope.save_service_time != "" && $scope.save_free_time!=""){
			$scope.discount = ($scope.save_service_price/($scope.save_service_time + $scope.save_free_time))/($scope.save_price/$scope.save_service_time) * 10;
		}
	}
	
	$scope.close = function(){
		$scope.ProductPriceList();
	}

});
});